﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Model.OrderSingleton
{
    public sealed class OrderSingleton
    {
        // 静态只读实例，在静态构造函数中初始化（饿汉式）
        private static readonly List<OrderSingletonModel> _instance;

        private static readonly object _lock = new object();

        // 静态构造函数：类加载时自动调用，保证线程安全
        static OrderSingleton()
        {
            _instance = new List<OrderSingletonModel>();
        }

        // 私有构造函数防止外部实例化
        private OrderSingleton() { }

        // 全局访问点（属性形式更符合 C# 习惯）
        public static List<OrderSingletonModel> Instance => _instance;

        // 线程安全的删除方法
        public static void RemoveByOrderId(string wo_code)
        {
            lock (_lock)
            {
                _instance.RemoveAll(order => order.OrderId.Equals(wo_code));
            }
        }

        public static void AddOrder(OrderSingletonModel order)
        {
            lock (_lock)
            {
                _instance.Add(order);
            }
        }

        public static void AddBatch(string wo_code,string btach)
        {
            lock (_lock)
            {
                foreach (var item in _instance)
                {
                    if (item.OrderId.Equals(wo_code))
                    {
                        item.Batch = btach;
                    }
                }
            }
        }

        public static int GetCount()
        {
            return _instance.Count;
        }
    }
}
